package com.ux.example
{
	import com.greensock.TweenMax;
	import com.ux.base.BLoader;
	import com.ux.event.BLoaderEvent;
	import com.ux.event.YouTubeEvent;
	import com.ux.sns.YouTubeList;
	import com.ux.sns.YouTubePlayer;
	import com.ux.util.MathUtil;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class YouTubeApiTest extends Sprite
	{
		private var loader : BLoader;
		private var selectedIndex : int = -1;
		private var list : YouTubeList;
		private var player : YouTubePlayer;
		private var isPlayerReady : Boolean = false;
		
		public function YouTubeApiTest()
		{
			super();
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			list = new YouTubeList();
			list.searchByUrl( "골프렛슨" );
			list.addEventListener(YouTubeEvent.XML_SEARCH_SET_COMPLETE, onXmlSetComplete );
			
			player = new YouTubePlayer();
			addChild( player );
			player.y = 150;
			player.addEventListener(YouTubeEvent.READY, onPlayerReady );
			player.addEventListener(YouTubeEvent.STATE_CHANGE, onStateChage );
		}
		
		
		private function onPlayerReady( e:YouTubeEvent ):void
		{
			isPlayerReady = true;
			player.width = stage.stageWidth;
			player.height = stage.stageHeight-160;
//			player.setPlaybackQuality( YouTubePlayer.QUALITY_HIGHRES );
		}
		
		private function onStateChage( e:YouTubeEvent ):void
		{
//			trace(" ==> YouTubeApiTest onStateChage : " + e.data );
			
			if( e.data == YouTubePlayer.PLAYING )
			{
				duraionSetting();
				btnSetting();
			}
		}
		
		
		private function onXmlSetComplete( e:YouTubeEvent ):void
		{
			
		}
		
		
		
		
		
		
		private function onThumbOver( e:MouseEvent ):void
		{
			var sp : Sprite = e.currentTarget as Sprite;
			TweenMax.to( sp, 0.5, {alpha:1} );
		}
		
		private function onThumbOut( e:MouseEvent ):void
		{
			var sp : Sprite = e.currentTarget as Sprite;
			TweenMax.to( sp, 0.5, {alpha:0.4} );
		}
		private var durationBar : Sprite;
		private var statusBar : Sprite;
		private var duration : Number;
		private function duraionSetting():void
		{
			if( durationBar == null )
			{
				durationBar = new Sprite();
				durationBar.graphics.beginFill(0x000000, 0.3 );
				durationBar.graphics.drawRect(0,0, player.width, 10 );
				durationBar.graphics.endFill();
				
				
				statusBar = new Sprite();
				statusBar.graphics.beginFill(0x0000ff, 1 );
				statusBar.graphics.drawRect(0,0,10,10 );
				statusBar.graphics.endFill();
			}
			
			addChild( durationBar );
			durationBar.addChild( statusBar );
			durationBar.addEventListener(MouseEvent.CLICK, onDurationBarClick );
			durationBar.y = player.y + player.height;
			addEventListener( Event.ENTER_FRAME, onEnter );
		}
		
		
		
		private function onDurationBarClick( e:MouseEvent ):void
		{
			var rate : Number = MathUtil.NumberToRate( durationBar.width, 0, durationBar.mouseX );
			trace(" ==> YouTubeApiTest onDurationBarClick : " + player.getDuration(), rate );

//			statusBar.width = durationBar.mouseX;
			
			player.seekTo( rate * player.getDuration() , true ); 
		}
		
		private function btnSetting():void
		{
			var btnURL : String = "http://tv01.search.naver.net/thumbnails?type=r150&q=http://cafefiles.naver.net/20100518_23/newturns302_1274155230844pv0NX_png/preview_newturns302.png";
			var btn : Sprite = new Sprite();
			addChild( btn );
			btn.buttonMode = true;
			btn.addEventListener(MouseEvent.CLICK, function():void
			{
				player.playVideo();
			});
			loader = new BLoader(btnURL, btn, onBtnLoad );
			
			function onBtnLoad():void
			{
				btn.x = 0;
				btn.y = player.y + player.height - btn.height;
			}
			
			var pauseURL : String = "http://tv01.search.naver.net/thumbnails?type=r150&q=http://blogfiles.naver.net/20100823_137/hrlovelove_1282554769138fjbi9_png/%C0%CF%BD%C3%C1%A4%C1%F6_hrlovelove.png";
			var pauseBtn : Sprite = new Sprite();
			addChild( pauseBtn );
			pauseBtn.buttonMode = true;
			pauseBtn.addEventListener(MouseEvent.CLICK, function():void
			{
				player.pauseVideo();
			});
			loader = new BLoader( pauseURL, pauseBtn, onPauseLoad );
			
			function onPauseLoad():void
			{
				pauseBtn.x = 150;
				pauseBtn.y = player.y + player.height - pauseBtn.height;
			}
			
			var soundURL : String = "http://tv01.search.naver.net/thumbnails?type=r150&q=http://blogfiles.naver.net/20100102_245/virgin0909_12624160311637ivmq_png/screen04_virgin0909.png";
			var soundBtn : Sprite = new Sprite();
			addChild( soundBtn );
			soundBtn.buttonMode = true;
			soundBtn.addEventListener(MouseEvent.CLICK, function():void
			{
				if( player.isMuted() )
				{
					player.unMute();
				}
				else
				{
					player.mute();
				}
			});
			loader = new BLoader( soundURL, soundBtn, onSoundLoad );
			
			function onSoundLoad():void
			{
				soundBtn.x = player.x + player.width - soundBtn.width;
				soundBtn.y = player.y + player.height - soundBtn.height;
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		private function onEnter( e:Event ):void
		{
			statusBar.width = durationBar.width * player.getCurrentTime()/player.getDuration(); 
		}
		
	}
}







